
package gui.std;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.GridLayout;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;

import javax.swing.BorderFactory;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JTextField;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

import util.Constants;

@SuppressWarnings("serial")
public class AppChooser extends JFrame {
	
	public AppChooser() {
		initialize();
	}
	
	private void initialize() {
		setSize(WIDTH, HEIGHT);
		setLocationRelativeTo(null);
		setResizable(false);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		
		getContentPane().add(chp = new ChooserPanel());
		setVisible(true);
	}
	
	public void setAction(ActionListener e) {
		chp.setAction(e);
	}
	
	public int getMode() {
		return chp.getMode();
	}
	
	public String getServer() {
		return chp.getServer();
	}
	
	public String getUser() {
		return chp.getUser();
	}
	
	public void setInfoMsg(String msg) {
		chp.setInfoMsg(msg);
	}
	
	private static final int WIDTH = 300;
	private static final int HEIGHT = 185;
	private ChooserPanel chp;
}

@SuppressWarnings("serial")
class ChooserPanel extends JPanel {
	
	public ChooserPanel() {
		initialize();
	}
	
	private void initialize() {
		setLayout(new BorderLayout());
		
		//center
		center = new JPanel(new BorderLayout());
		JPanel radios = new JPanel(new BorderLayout());
		server = new JRadioButton("Server");
		server.setFocusable(false);
		client = new JRadioButton("Client", true);
		client.setFocusable(false);
		
		client.addChangeListener(new ChangeListener(){
			public void stateChanged(ChangeEvent e) {
				clinfo.setVisible(client.isSelected());
			}
		});
		
	    bg = new ButtonGroup();
	    
	    server.setFont(Constants.DEFAULT_FONT);
	    client.setFont(Constants.DEFAULT_FONT);
	    
	    bg.add(server);
	    bg.add(client);
	    radios.add(server, BorderLayout.NORTH);
	    radios.add(client);
	    
	    //client information
	    clinfo = new JPanel(new GridLayout(2, 2, -90, 2));
	    lserver = new JLabel("Server:");
	    lid = new JLabel("Id:");
	    iserver = new JTextField("localhost");
	    iid = new JTextField();
	    
	    clinfo.setFont(Constants.DEFAULT_FONT);
	    lserver.setFont(Constants.DEFAULT_FONT);
	    lid.setFont(Constants.DEFAULT_FONT);
	    iserver.setFont(Constants.DEFAULT_FONT);
	    iserver.setColumns(20);
	    iserver.setBorder(BorderFactory.createLineBorder(Color.GRAY));
	    iserver.setBackground(Constants.DEFAULT_BACKGROUND_ITEM_COLOR);
	    iid.setFont(Constants.DEFAULT_FONT);
	    iid.setBackground(Constants.DEFAULT_BACKGROUND_ITEM_COLOR);
	    iid.setBorder(BorderFactory.createLineBorder(Color.GRAY));
	    
	    KeyAdapter kAdapter = new KeyAdapter(){
	    	public void keyTyped(KeyEvent e){
	    		if (e.getKeyChar() == '\n') {
	    			accept.doClick();
	    		}
	    	}
	    };
	    
	    iserver.addKeyListener(kAdapter);
	    iid.addKeyListener(kAdapter);
	    
	    clinfo.add(lserver);
	    clinfo.add(iserver);
	    clinfo.add(lid);
	    clinfo.add(iid);
	    JPanel aux = new JPanel();
	    aux.add(clinfo);
	    radios.add(aux, BorderLayout.SOUTH);
	    center.add(radios);
		
		//south
		south = new JPanel(new BorderLayout(2, 2));
		accept = new JButton("Accept");
		accept.setFocusable(false);
		accept.setFont(Constants.DEFAULT_FONT);
		accept.setBackground(Constants.DEFAULT_BACKGROUND_ITEM_COLOR);
		
		info = new JLabel(" Done!", JLabel.CENTER);
		info.setForeground(Color.RED);
		info.setFont(Constants.DEFAULT_FONT);
		
		JPanel accept_ = new JPanel();
		accept_.add(accept);
		
		south.add(accept_);
		south.add(info, BorderLayout.SOUTH);
		
		add(center);
		add(south, BorderLayout.SOUTH);
	}

	void setAction(ActionListener e) {
		accept.addActionListener(e);
	}
	
	int getMode() {
		return (server.isSelected()) ? Constants.SERVER : Constants.CLIENT;
	}
	
	public String getServer() {
		return iserver.getText().trim();
	}
	
	public String getUser() {
		return iid.getText().trim();
	}
	
	public void setInfoMsg(String msg) {
		info.setText(msg);
	}
	
	private JPanel center;
	private JPanel south;
	private JPanel clinfo;
	private JButton accept;
	private ButtonGroup bg;
	private JRadioButton server;
	private JRadioButton client;
	private JLabel lserver;
	private JLabel lid;
	private JLabel info;
	private JTextField iserver;
	private JTextField iid;
}
